﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IceDog.WebApi.AnythingApi.Mapping;
using IceDog.WebApi.AnythingApi.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace IceDog.WebApi.AnythingApi.Controllers
{
    [Route("test/[controller]/[action]")]
    [Route("[area]/test/[controller]/[action]")]
    [Area("any")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36";
        private int timeout = 60000;
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult Ping()
        {
            var url = $"{WebHelper.GetOrigin()}/search/index.html";
            string result = null;
            WebHelper.CreateGetHttpResponse(url, timeout, userAgent, Encoding.UTF8, ref result, ContentTypeHelper.GetMimeType(".html"));
            bool isOk = result.IndexOf("<!DOCTYPE html>") > -1;

            return Ok(new { ok = isOk });
        }

        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult Ping2()
        {

            string result = Task.Factory.StartNew(() =>
            {
                string text = "";
                var url = $"{WebHelper.GetOrigin()}/search/index.html";
                WebHelper.CreateGetHttpResponse(url, timeout, userAgent, Encoding.UTF8, ref text, ContentTypeHelper.GetMimeType(".html"));
                return text;
            }).Result;
            bool isOk = result.IndexOf("<!DOCTYPE html>") > -1;

            return Ok(new { ok = isOk });
        }

        [HttpGet]
        public ActionResult AddIdx()
        {
            var isOk = true;
            var baseUrl = WebHelper.GetOrigin();
            string indexName = "kmindex";

            var url = $"{baseUrl}/search/IndexDocument/WriteDocumentAsync";
            string data = "{\"catalogId\":\"4674490e-67a8-479a-846e-29a312433de2\",\"repositoryId\":\"ace4b650-c0b2-45c5-9d01-2640def28da2\",\"catalogName\":\"公共1\",\"catalogContent\":\"<p>公共1公共1公共1公共1</p>\n<p>花园花园花园花园花园花园花园花园花园花园花园花园花园花园花园花园花园花园花园</p>\",\"creatorId\":\"90eb0c6350ce4284828e633304decc01\",\"updaterId\":\"90eb0c6350ce4284828e633304decc01\",\"creatorName\":\"Administrator\",\"updaterName\":\"Administrator\",\"createTime\":\"2021-01-22 18:16:20\",\"updateTime\":\"2021-01-22 18:30:11\",\"locatePathId\":\"6a5458f9-22aa-4f23-98d8-19975e5d4496/d0836208-a8a9-4dc8-b409-28fddca9429b/4674490e-67a8-479a-846e-29a312433de2\"}";
            var cookies = WebHelper.GetCookies();
            var token = string.Empty;
            var checkToken = string.Empty;
            if (cookies.ContainsKey("token"))
            {
                token = cookies["token"];
            }
            if (cookies.ContainsKey("checkToken"))
            {
                checkToken = cookies["checkToken"];
            }
            var paramSets = new Dictionary<string, string>()
            {
                ["IndexName"] = indexName,
                ["Data"] = data,
                ["Id"] = "4674490e-67a8-479a-846e-29a312433de2"
            };
            var cookies2 = new Dictionary<string, string>() {
                {"token",token },
                {"checkToken",checkToken }
            };
            string result = "";
            WebHelper.CreatePostHttpResponse(url, paramSets, timeout, userAgent, Encoding.UTF8, ref result, cookies: cookies2);
            var resultObj = JsonConvert.DeserializeObject(result) as JObject;
            return Ok(new { ok = isOk });
        }
    }
}
